package com.zy.explore.wechat.service;

import com.zy.explore.wechat.domain.mongo.dao.MongoExampleDao;
import com.zy.explore.wechat.domain.mongo.entity.MongoExampleEntity;
import com.zy.explore.wechat.frm.CommonStringIdFrm;
import com.zy.explore.wechat.frm.ExampleFrm;
import com.zy.explore.wechat.vo.ExampleVO;
import com.zy.explore.wechat.vo.PageVO;
import com.zy.explore.wechat.assembler.MongoExampleAssembler;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * description Mongo示例Service
 *
 * @author ZY
 * @date 2021年05月19日 20:57
 */
@Service
public class MongoExampleService {

    @Resource
    private MongoExampleDao mongoExampleDao;

    /**
     * 新增数据
     *
     * @param frm 参数对象
     * @return 视图对象
     */
    public ExampleVO insert(ExampleFrm frm) {
        MongoExampleEntity entity = MongoExampleAssembler.convertFromFrm(frm);
        entity.setCreateTime(new Date());
        MongoExampleEntity res = mongoExampleDao.insert(entity);
        if (res != null) {
            return MongoExampleAssembler.convertToVO(res);
        }
        return null;
    }

    /**
     * 删除对象
     *
     * @param frm 参数对象
     * @return 1:删除成功 其他:删除失败
     */
    public Integer delete(CommonStringIdFrm frm) {
        mongoExampleDao.remove(frm.getId());
        return 1;
    }

    /**
     * 更新对象
     *
     * @param frm 待更新的Frm对象
     * @return 成功返回更新后的VO对象，失败返回null
     */
    public ExampleVO update(ExampleFrm frm) {
        MongoExampleEntity entity = MongoExampleAssembler.convertFromFrm(frm);
        entity.setUpdateTime(new Date());
        int res = mongoExampleDao.updateById(entity);
        if (res == 1) {
            return MongoExampleAssembler.convertToVO(entity);
        }
        return null;
    }

    /**
     * 根据ID查询数据
     *
     * @param frm 主键Frm对象
     * @return 成功返回对应ID的VO对象，失败返回null
     */
    public ExampleVO findById(CommonStringIdFrm frm) {
        MongoExampleEntity entity = mongoExampleDao.findById(frm.getId());
        if (entity != null) {
            return MongoExampleAssembler.convertToVO(entity);
        }
        return null;
    }

    /**
     * 根据条件查询数据集合
     *
     * @param frm 查询条件对象
     * @return 满足条件的数据集合
     */
    public List<ExampleVO> getList(ExampleFrm frm) {
        Query query = MongoExampleAssembler.convertToQuery(frm);
        List<MongoExampleEntity> list = mongoExampleDao.findList(query);
        if (!CollectionUtils.isEmpty(list)) {
            List<ExampleVO> retList = new ArrayList<>();
            list.forEach(x -> retList.add(MongoExampleAssembler.convertToVO(x)));
            return retList;
        }
        return new ArrayList<>();
    }

    /**
    * 根据条件查询分页
    *
    * @param frm 查询条件
    * @return 满足条件的分页
    */
    public PageVO<ExampleVO> getPageInfo(ExampleFrm frm) {
        Query query = MongoExampleAssembler.convertToQuery(frm);
        long count = mongoExampleDao.getCount(query);
        query.skip((long) (frm.getPageNum() - 1) * frm.getPageSize());
        query.limit(frm.getPageSize());
        List<MongoExampleEntity> list = mongoExampleDao.findList(query);

        PageRequest pageRequest = PageRequest.of(frm.getPageNum(), frm.getPageSize());
        Page<ExampleVO> retPage = new PageImpl<>(new ArrayList<>(), pageRequest, count);
        if (CollectionUtils.isEmpty(list)) {
            return new PageVO<>(retPage);
        }
        List<ExampleVO> retList = new ArrayList<>();
        list.forEach(x -> retList.add(MongoExampleAssembler.convertToVO(x)));
        retPage = new PageImpl<>(retList, pageRequest, count);
        return new PageVO<>(retPage);
    }

}
